////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: TileMapUtils.java
//	Originating Author: Jon Whitefield
//	
//	Description:
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.world.level;

public class TileMapUtils {
	
	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	/**
	 * Creates a new map of the specified dimensions with the default tile reference set to every tile
	 */
	public static String[][] newTileMap( int width, int height, String defaultTileReference ) {
		
		//Ensure the max height is within range
		height = Math.max(1, height);
		
		//Ensure max width is within range
		width = Math.max(1, width);
		
		//New map requested
		String[][] newMap = new String[ height ][ width ];
		
		//Loop through the new map setting the default tiles
		for( int i = 0; i < newMap.length; i++ )
			for( int k = 0; k < newMap[i].length; k++ )
				newMap[i][k] = defaultTileReference;
		
		//Return the new map
		return newMap;
	}
	
	/**
	 * Add new column to the map
	 * 
	 * @param beforeIndex the column index which is being added
	 */
	public static String[][] addColumn(String[][] sourceMap, int beforeIndex, String defaultTileReference ) {
		
		//If the index is less then zero set it to zero
		if( beforeIndex < 0 )
			beforeIndex = 0;
		
		//Create a new map which is one column longer then the previous one 
		String[][] newMap = newTileMap( sourceMap.length, sourceMap[0].length + 1, defaultTileReference ) ;
		
		//Keep an index of where we are looking on the old map 
		int oldMapIndex  = 0;		
		
		//Loop through each of the tiles in the map moving the old tiles to the new map
		for( int i = 0; i < newMap.length; i++ ) {
			for( int k = 0; k < newMap[i].length; k++ ) {
			
				//Check the old map index is still inbounds
				if( oldMapIndex < sourceMap.length )	{
					
					//Copy the map into the new map
					newMap[i][k] = ( k == beforeIndex )? defaultTileReference:sourceMap[i][ oldMapIndex ];					
				}
				else {

					//If the old map index is out of bounds it means the new column is on the end
					newMap[i][k] = defaultTileReference;
				}
				
				//If this isn't the column we created up the old map index
				if( beforeIndex != k )
					oldMapIndex++;
			}
			
			//Reset the old map index
			oldMapIndex = 0;
		}
		
		//Return the new map
		return newMap;
	}
	
	/**
	 * Add a new row to the map 
	 * 
	 * @param beforeIndex	the row index which is being added
	 */
/*	public void addRow( int beforeIndex, String defaultTileReference ) {
		
		//If the index is less then zero set it to zero
		if( beforeIndex < 0 )
			beforeIndex = 0;
		
		//Create a new map which is one row longer then the previous one 
		String[][] newMap = new String[ this.map.length + 1 ][ this.map[0].length ];
				
		//Keep an index of where we are looking on the old map 
		int oldMapIndex  = 0;
		
		//Loop through each of the tiles in the map moving the old tiles to the new map
		for( int i = 0; i < newMap.length; i++ ) {
			for( int k = 0; k < newMap[i].length; k++ ) {
			
				//Check that that the old map index is still in bounds
				if( oldMapIndex < this.map.length )
				{
					//Copy the map into the new map
					newMap[i][k] = ( i == beforeIndex )? defaultTileReference:this.map[ oldMapIndex ][k];					
				}
				else
				{
					//If the old index is equal to the length of the old map it means our 
					//new row is on the end of the map 
					newMap[i][k] = defaultTileReference;
				}
			}
			
			//Up the old map index so other tiles will be added after this row
			if( i != beforeIndex )
				oldMapIndex++;
		}
		
		//Set the new map
		this.map = newMap;
	}*/
	
	//----------------------------------
	// Private
	//----------------------------------
	
	//----------------------------------
	// Overridden / Implemented
	//----------------------------------
}